package de.gameduell.framework.game.billard.model {
	import de.gameduell.framework.game.billard.controller.BillardEngine;
	import de.gameduell.framework.geom.Geometry;
	
	import org.cove.ape.APEngine;
	import org.cove.ape.Clonable;
	import org.cove.ape.RectangleParticle;
	
	import flash.display.Sprite;
	import flash.geom.Rectangle;	

	/**
	 * @author cbra
	 */
	public class Cushion extends RectangleParticle implements Clonable{
		
		private var _extendedBoundingBox:Rectangle;
		
		public function Cushion(apEngine:BillardEngine,x:Number = 0, y:Number = 0, width:Number = 0, height:Number = 0, rotation:Number = 0, 
										   fixed:Boolean = false,mass:Number = 1, elasticity:Number = 0.3,friction:Number = 0)  {
			super(apEngine,x,y,width,height,rotation,fixed,mass,elasticity,friction);
		}
		
		public static function createParticle(apEngine:BillardEngine,aSprite:Sprite):Cushion {	
			var rotationAngle:Number = aSprite.rotation;
			var rotationRad:Number = Geometry.angleToArc( rotationAngle);
				
			aSprite.rotation = 0;
			var cushion:Cushion = new Cushion(apEngine,aSprite.x, aSprite.y,aSprite.width,aSprite.height,rotationRad,true);
			
			aSprite.rotation = rotationAngle;
			apEngine.addCushion(cushion);
			
			// FIXME hardcoded size (will be fixed by ferry)
			var extendedBoundingBox:Rectangle = aSprite.getBounds(aSprite.parent);
			extendedBoundingBox.inflate(10,10);
			cushion.extendedBoundingBox = extendedBoundingBox;
			return cushion;
		}
		
		public function copyCushion(cushion:Cushion):void{
			copyRectangle(cushion);
			_extendedBoundingBox = new Rectangle(cushion._extendedBoundingBox.x,cushion._extendedBoundingBox.y,cushion._extendedBoundingBox.width,cushion._extendedBoundingBox.height);
		}
		
		override public function clone(aEngine:APEngine):Clonable {
			var clone:Cushion = new Cushion(BillardEngine(aEngine));
			clone.copyCushion(this);
			return clone;	
		}

		public function set extendedBoundingBox(aBox:Rectangle):void{
			 _extendedBoundingBox = aBox;
		}
		
		public function get extendedBoundingBox():Rectangle{
			return _extendedBoundingBox;
		}
	
	}
}
